package com.lau.config;

import com.lau.common.enums.StatusCode;
import com.lau.common.lang.Const;
import com.lau.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author:Lau
 * @create: 2023-12-20 13:18
 * @Description: 设置自动填充表的创建人、创建时间、修改人、修改时间
 */
@Component
public class MyMetaObjectHandle implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createId", SecurityUtils.getUserId(),metaObject);
        this.setFieldValByName("createTime",LocalDateTime.now(),metaObject);
        this.setFieldValByName("updateId",SecurityUtils.getUserId(),metaObject);
        this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        //默认启用状态
        this.setFieldValByName("status", StatusCode.STATUS_NORMAL.getCode(),metaObject);
        //默认正常状态
        this.setFieldValByName("delFlag", Const.STATUS_NORMAL,metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateId",SecurityUtils.getUserId(),metaObject);
        this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
    }
}
